<template>
	<!-- 隐患整改下发页面 -->
	<view class="workApply-container" :style="{ height: `${windowHeight}px` }">
		<!-- 基础表单校验 -->
		<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" border>
			<uni-forms-item label="整改部门：" required name="hiddenDangerDept">

				<custom-tree-select :listData="treeData" dataLabel="label" dataValue="id" :search="true"
					:mutiple="false" v-model="valiFormData.hiddenDangerDept" :showChildren="true" @input="depChange" />

				</custom-tree-select>

			</uni-forms-item>

			<uni-forms-item label="公司整改人：" required name="companyHeadId" class="view-select">
				<!-- :arrayConfig="configParams" -->
				<uni-data-select v-model="valiFormData.companyHeadId" :localdata="rectUserList"
					></uni-data-select>

			</uni-forms-item>
			<!-- 上面选择检查计划回写上来的 -->
			<uni-forms-item label="部门整改人：" required name="departmentHeadId">
				<custom-tree-select :listData="deptUserList" dataLabel="nickName" dataValue="userId" :search="true"
					:mutiple="false" v-model="valiFormData.departmentHeadId" />
				</custom-tree-select>
			</uni-forms-item>


		</uni-forms>

		<button type="primary" @click="submit('valiForm')" class="submit-btn">
			确认
		</button>


	</view>
</template>

<script>
	import widowHeightMixins from "@/mixins/widowHeightMixins.js";

	import {
		getNowTime
	} from "@/utils/nowTime.js"
	import {
		addAllShoot,
	} from "@/api/hiddenTrouble/allShoot.js";
	import {
		distributeRect,
	} from "@/api/hiddenTrouble/rectification.js";
	import {
		listUser,
		allUser
	} from "@/api/system/user.js"
	import {
		deptTreeSelect
	} from "@/api/system/user";
	import {
		mapGetters
	} from "vuex"
	export default {
		mixins: [widowHeightMixins],
		components: {

		},
		onLoad(e) {
			// console.log(e, "e");
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('acceptDataFromOpenedPage', (data) => {
				this.queryForm.pageStatus = data.pageStatus
				this.checkId = data.checkId
				console.log("接受", data)
			});
			// 动态设置标题
			uni.setNavigationBarTitle({
				title: `隐患整改--下发`,
			});
		},
		data() {
			return {
				//部门下用户列表
				deptUserList: [],
				//人员的格式化
				configParams: {
					valueKey: "userId",
					labelKey: "nickName"
				},
				//隐患登记
				hiddenForm: {},
				//整改人
				rectUserList:[],
				//详情
				detailObj: {},
				hiddenLevelOption: [{
						text: '一般隐患',
						value: 1
					},
					{
						text: '重大隐患',
						value: 2
					},

				],
				solveOption: [{
						text: '是',
						value: 1
					},
					{
						text: '否',
						value: 2
					},
				],
				//隐患登记表
				hiddentroubleInspectionRegisters: [],
				//当前选中
				current: 0,

				//选中ID
				checkId: null,
				//操作状态
				operateStatus: null,
				//上传参数对象
				queryForm: {},
				//手工选择弹窗
				manualVisible: false,
				//被检查单位的数据
				treeData: [],
				// 校验表单数据
				valiFormData: {},
				taskLevelOption: [],

				//检查表内容
				tableData: [{
						id: "999",
						inspectionScopeName: "其他",
						inspectionCategoryName: "其他",
					},

				],
				localRes:{},
				// 数据总量
				total: 0,
				loading: false,
				//检查执行人
				executorList: [],


				// 校验规则
				rules: {
					dept: {
						rules: [{
							required: true,
							errorMessage: "姓名不能为空",
						}, ],
					},
					name: {
						rules: [{
							required: true,
							errorMessage: "姓名不能为空",
						}, ],
					},
					age: {
						rules: [{
								required: true,
								errorMessage: "年龄不能为空",
							},
							{
								format: "number",
								errorMessage: "年龄只能输入数字",
							},
						],
					},
				},
			};
		},
		methods: {
			/** 部门的change事件 */
			async depChange(val) {
				console.log("组件选择", val);
				// this.selectId = val.id;
				let query = {
					deptId: val[0],

				};
				let res = await listUser(query);
				this.deptUserList = res.rows;

			},
			
			//获取公司整改人
			async getRectUser(){
				let query = {
					companyAccount: this.localRes.companyAccount
				}
				let res = await allUser(query)
				this.rectUserList = res.rows.map(item => {
					return {
						text: item.nickName +'-' + item.deptName,
						value: item.userId

					}
				})
			},

			//处理详情接口数据回写上面
			handleopen(data) {
				
			},
			/** 被检查部门部门下拉树结构 */
			async getDeptTree() {
				const response = await deptTreeSelect();
				this.treeData = response.data;
			},


			init() {

				let res = uni.getStorageSync('userInfo')
				this.localRes = res
				this.$set(this.valiFormData, "companyHeadId", this.localRes.userId)

			},
			submit(ref) {
				this.$refs[ref]
					.validate()
					.then(async (res) => {
						this.valiFormData.departmentHeadId = this.valiFormData.departmentHeadId[0]
						this.valiFormData.hiddenDangerDept = this.valiFormData.hiddenDangerDept[0]
						this.valiFormData.id = this.checkId
						let response = await distributeRect(this.valiFormData);
						uni.showToast({
							title: `提交成功`,
						});
						uni.navigateTo({
							url: "/pages/trouble/index",
							success: (res) => {
								res.eventChannel.emit('acceptDataFromOpenerPage', { page: 'manage' })
							}
						})


					})
					.catch((err) => {
						console.log("err", err);
					});
			},
		},
		mounted() {
			this.getDeptTree()
			this.init()
			this.getRectUser()
		},
		computed: {
			...mapGetters(["allUsers", "deptId"]),
		}
	};
</script>

<style lang="scss" scoped>
	.workApply-container {
		padding: 20rpx;
		background-color: #fff;
		overflow-y: auto;

		::v-deep {
			.view-select{
				.uni-select__selector{
					top:66rpx;
				}
			}
			.uni-forms-item--border {
				border-top: 0 solid #000;
				border-bottom: 2rpx solid #e8eaec;
			}

			.uni-forms-item__label {
				width: 40% !important;
			}

			.submit-btn {
				margin-top: 30rpx;
				background: #137ee9;
				border-radius: 10rpx;
			}
		}
	}
</style>